/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pt.iasd.youthshelter.wizards.core.actions;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.netbeans.api.progress.ProgressHandle;
import org.netbeans.api.progress.ProgressHandleFactory;
import org.openide.DialogDescriptor;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionRegistration;
import org.openide.util.Lookup;
import pt.iasd.youthshelter.datacore.controller.exceptions.NonexistentEntityException;
import pt.iasd.youthshelter.datamodel.Investiture;
import pt.iasd.youthshelter.datamodel.service.InvestitureController;
import pt.iasd.youthshelter.wizards.core.events.investiture.InvestitureTablePanel;

@ActionID(
        category = "Management",
        id = "pt.iasd.youthshelter.wizards.core.actions.ShowInvestiturePanelAction")
@ActionRegistration(
        iconBase = "pt/iasd/youthshelter/wizards/core/actions/icon.png",
        displayName = "Investitures")
@ActionReferences({
    @ActionReference(path = "Menu/Management", position = 300),
    @ActionReference(path = "Toolbars/Management", position = 300)
})
public final class ShowInvestiturePanelAction implements ActionListener {
    private static final ResourceBundle bundle = java.util.ResourceBundle.getBundle("pt/iasd/youthshelter/wizards/core/Bundle");
    /* Logging object. */
    private Logger logger = Logger.getLogger(this.getClass().getName());

    @Override
    public void actionPerformed(ActionEvent e) {
        ProgressHandle pHandle = ProgressHandleFactory.createHandle(
                    bundle.getString("ProgressBarWizard"));
        pHandle.start();
        final InvestitureTablePanel investiturePanel = new InvestitureTablePanel();
        DialogDescriptor dialog = new DialogDescriptor(investiturePanel, bundle.getString("Investiture.Panel"), true, new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                if(e.getSource() == DialogDescriptor.OK_OPTION) {
                    InvestitureController controller = Lookup.getDefault().lookup(InvestitureController.class);
                    SimpleDateFormat formatter = new SimpleDateFormat("dd-mm-yyyy");
                    for(Investiture investiture : investiturePanel.getCompleteInvestitures()) {
                        if(investiture.getId() == null) {
                            try {
                                controller.create(investiture);
                            } catch (Exception ex) {
                                logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
                                NotifyDescriptor dialog = new NotifyDescriptor.Message(
                                    String.format(bundle.getString("Investiture.Save.Error")
                                                .replace("\n", System.getProperty("line.separator")),
                                            investiture.getPathfinderId().getName(), investiture.getLiderId().getName(),
                                            investiture.getPlace(), formatter.format(investiture.getEventDate()), ex.getLocalizedMessage()),
                                    NotifyDescriptor.ERROR_MESSAGE);
                                DialogDisplayer.getDefault().notify(dialog);
                            }
                        }
                    }
                    for(Investiture investiture : investiturePanel.getEditedInvestitures()) {
                        try {
                            controller.edit(investiture);
                        } catch (Exception ex) {
                            logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
                            NotifyDescriptor dialog = new NotifyDescriptor.Message(
                                String.format(bundle.getString("Investiture.Edit.Error")
                                        .replace("\n", System.getProperty("line.separator")),
                                    investiture.getPathfinderId().getName(), investiture.getLiderId().getName(),
                                    investiture.getPlace(), formatter.format(investiture.getEventDate()), ex.getLocalizedMessage()),
                                NotifyDescriptor.ERROR_MESSAGE);
                            DialogDisplayer.getDefault().notify(dialog);
                        }/*
                        } else {
                            try {
                                controller.create(investiture);
                            } catch (Exception ex) {
                                logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
                                NotifyDescriptor dialog = new NotifyDescriptor.Message(
                                    String.format(bundle.getString("Investiture.Save.Error"),
                                        investiture.getPathfinderId().getName(), investiture.getLiderId().getName(),
                                        investiture.getPlace(), formatter.format(investiture.getEventDate()), ex.getLocalizedMessage()),
                                    NotifyDescriptor.ERROR_MESSAGE);
                                DialogDisplayer.getDefault().notify(dialog);
                            }
                        }*/
                    }
                    for(Investiture investiture : investiturePanel.getRemovedInvestitures()) {
                        try {
                            controller.destroy(investiture.getId());
                        } catch (NonexistentEntityException ex) {
                            logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
                            NotifyDescriptor dialog = new NotifyDescriptor.Message(
                                String.format(bundle.getString("Investiture.NotExist")
                                            .replace("\n", System.getProperty("line.separator")),
                                        investiture.getPathfinderId().getName(), investiture.getLiderId().getName(),
                                        investiture.getPlace(), formatter.format(investiture.getEventDate())),
                                NotifyDescriptor.ERROR_MESSAGE);
                            DialogDisplayer.getDefault().notify(dialog);
                        }
                    }
                }
            }
        });
        pHandle.finish();
        DialogDisplayer.getDefault().notify(dialog);
    }
}
